import { Toast } from 'mint-ui';
import axios from "axios"
import {Tools} from "../../lib/tools";
import * as types from '../types'

const state = {
    inputValue:"",
    CodeInputValue:"",
    totalTime: '60',
    canClick: true,//添加canClick
    codaData:"",
    getCode:"获取验证码",
    btnDisabled: true,
    authCodeDisabled: true,
    lastData:{
        "AREADetail": [{
            "AREADetail": {
                "AREANAME": "崇明县",
                "AREACODE": "310230"
            }
        }, {
            "AREADetail": {
                "AREANAME": "崇明区",
                "AREACODE": "310151"
            }
        }, {
            "AREADetail": {
                "AREANAME": "奉贤区",
                "AREACODE": "310120"
            }
        }, {
            "AREADetail": {
                "AREANAME": "南汇区",
                "AREACODE": "310119"
            }
        }, {
            "AREADetail": {
                "AREANAME": "青浦区",
                "AREACODE": "310118"
            }
        }, {
            "AREADetail": {
                "AREANAME": "松江区",
                "AREACODE": "310117"
            }
        }, {
            "AREADetail": {
                "AREANAME": "金山区",
                "AREACODE": "310116"
            }
        }, {
            "AREADetail": {
                "AREANAME": "浦东新区",
                "AREACODE": "310115"
            }
        }, {
            "AREADetail": {
                "AREANAME": "嘉定区",
                "AREACODE": "310114"
            }
        }, {
            "AREADetail": {
                "AREANAME": "宝山区",
                "AREACODE": "310113"
            }
        }, {
            "AREADetail": {
                "AREANAME": "闵行区",
                "AREACODE": "310112"
            }
        }, {
            "AREADetail": {
                "AREANAME": "杨浦区",
                "AREACODE": "310110"
            }
        }, {
            "AREADetail": {
                "AREANAME": "虹口区",
                "AREACODE": "310109"
            }
        }, {
            "AREADetail": {
                "AREANAME": "闸北区",
                "AREACODE": "310108"
            }
        }, {
            "AREADetail": {
                "AREANAME": "普陀区",
                "AREACODE": "310107"
            }
        }, {
            "AREADetail": {
                "AREANAME": "静安区",
                "AREACODE": "310106"
            }
        }, {
            "AREADetail": {
                "AREANAME": "长宁区",
                "AREACODE": "310105"
            }
        }, {
            "AREADetail": {
                "AREANAME": "徐汇区",
                "AREACODE": "310104"
            }
        }, {
            "AREADetail": {
                "AREANAME": "卢湾区",
                "AREACODE": "310103"
            }
        }, {
            "AREADetail": {
                "AREANAME": "黄浦区",
                "AREACODE": "310101"
            }
        }]
    }

}

const getters ={

}

const actions = {
    [types.LOGIN_SEND_AUTH_CODE] ({state}) {
        var r = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if ( !r.test(state.inputValue) ) {
            Toast('手机号格式错误!')
            return
        }
    }



}

const mutations ={
    getInputValue(state,value){
        state.inputValue=value
    },
    getCodeInputValue(state,value){
        state.CodeInputValue=value
},
    countDownData(state){
        var r = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        var that=this
    if (r.test(state.inputValue)){
        axios.post('http://www.supdata.com:20780/shangwei-innfront/shangWeiWxFront/doProcess',
            {
                "wxRequestDate": (new Date()).format("yyyyMMddhhmmss"),
                "service": "verifycodeForWx",
                "mobile": this.inputValue
            }
        ).then(
            function (data) {
                if (data.responseCode == "0000"){
                    // this.$router.push('/LegalPersonPhoto')
                    state.canClick = false
                    state.getCode = state.totalTime + 's'
                    let clock = window.setInterval(() => {
                        state.totalTime--
                        state.getCode = state.totalTime + 's'
                        if (state.totalTime < 0) {
                            window.clearInterval(clock)
                            state.getCode = '重新发送验证码'
                            state.totalTime = 60;
                            state.canClick = true  //这里重新开启
                        }
                    }, 1000)

                }else if(data.responseCode == "555555"){
                    Toast('请求超时，请重新登录!')
                }
            }
        ).catch()

        return false
    }
        Toast("请输入正确手机号码")
},
    [types.LOGIN_NEXT_BUTTON_CHECK] ( state ) {
        if ( Tools.check.isEmpty(state.inputValue) ||
            Tools.check.isEmpty(state.CodeInputValue)) {
            state.btnDisabled = true
        } else state.btnDisabled = false;
    },
    [types.AUTH_CODE_BUTTON_CHECK] ( state ) {
        if ( Tools.check.isEmpty(state.inputValue)) {
            state.authCodeDisabled = true
        } else state.authCodeDisabled = false;
    }
};

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}